package com.pn.controller;

import com.pn.entity.CurrentUser;
import com.pn.entity.Product;
import com.pn.entity.Result;
import com.pn.page.Page;
import com.pn.service.*;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Collections;
import java.util.List;

@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    private StoreService storeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private SupplyService supplyService;
    @Autowired
    private PlaceService placeService;
    @Autowired
    private UnitService unitService;
    @Autowired
    private TokenUtils tokenUtils;


    /*查询所有仓库的url接口/product/store-list*/
    @RequestMapping("/store-list")
    public Result storeList() {
        return Result.ok(storeService.queryAllStore());
    }

    /*查询所有品牌的url接口*/
    @RequestMapping("/brand-list")
    public Result brandList() {
        return Result.ok(brandService.queryAllBrand());
    }

    /*分页查询商品列表*/
    @RequestMapping("/product-page-list")
    public Result productPageList(Page page, Product product) {
        return Result.ok(productService.queryProductPage(page, product));
    }

    /*查询所有商品分类树url接口product/category-tree*/
    @RequestMapping("category-tree")
    public Result typeTree() {
        return Result.ok(productTypeService.productTypeTree());
    }

    /*查询所有供应商url接口supply-list*/
    @RequestMapping("/supply-list")
    public Result supplyList() {
        return Result.ok(supplyService.queryAllSupply());
    }

    /*查询所有产地url接口/place-list*/
    @RequestMapping("/place-list")
    public Result placeList() {
        return Result.ok(placeService.queryAllPlace());
    }

    /*查询所有单位url接口/unit-list*/
    @RequestMapping("/unit-list")
    public Result unitList() {
        return Result.ok(unitService.queryAllUnit());
    }

    @Value("${file.upload-path}")
    private String uploadPath;

    /*上传商品图片的url接口/img-upload*/
    @CrossOrigin
    @RequestMapping("/img-upload")
    public Result uploadImage(MultipartFile file) {
        try {

            /*
            拿到图片上传到的目录路径File对象 -- classpath:static/img/upload
             */
            File uploadDirFile = ResourceUtils.getFile(uploadPath);

            // 拿到图片上传到的目录路径的磁盘路径
            String uploadDirPath = uploadDirFile.getAbsolutePath();
            // 拿到上传图片的名称
            String filename = file.getOriginalFilename();
            // 拿到上传的文件要保存的磁盘文件的路径
            String uploadFilePath = uploadDirPath + "\\" + filename;
            // 上传图片
            file.transferTo(new File(uploadFilePath));

            // 成功响应
            return Result.ok("图片上传成功！");
        } catch (Exception e) {
            return Result.err(Result.CODE_ERR_BUSINESS, "图片上传失败！");
        }
    }

    /*添加商品的url接口/product-add*/
    @RequestMapping("/product-add")
    public Result addProduct(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int creatBy = currentUser.getUserId();
        product.setCreateBy(creatBy);
        return productService.addProduct(product);
    }

    /*修改商品上下架状态url接口/state-change*/
    @RequestMapping("/state-change")
    public Result updateProductState(@RequestBody Product product) {
        return productService.setProductState(product);
    }

    /*删除单个商品url接口/product-delete*/
    @RequestMapping("/product-delete/{productId}")
    public Result removeProduct(@PathVariable Integer productId) {
        return productService.removeProductByIds(Collections.singletonList(productId));
    }

    /*删除多个商品url接口/product-list-delete*/
    @RequestMapping("/product-list-delete")
    public Result removeProductByIds(@RequestBody List<Integer> productIds){
        return productService.removeProductByIds(productIds);
    }

    /*修改商品信息url接口/product-update*/
    @RequestMapping("/product-update")
    public Result setProduct(@RequestBody Product product,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        product.setCreateBy(currentUser.getUserId());
        return productService.setProductById(product);
    }
}
